import http from "../../requrt/http";

// pages/goodsbux/goodsbux.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: []
  },
  totalPages: 1,
  query: {
    cid: '',
    query: '',
    pagenum: 1,
    pagesize: 5,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options, '12321321');
    this.query.cid = options.id
    this.query.query = options.query || "";
    console.log(this.query.cid, '213213213s');
    console.log(this.query.query, '2222');
  },

  async getdata() {
    let res = await http('goods/search', 'GET', {
      cid: this.query.cid,
      pagenum: this.query.pagenum,
      pagesize: this.query.pagesize
    })
    const total = res.total;
    // 计算总页数
    this.totalPages = Math.ceil(total / this.query.pagesize);
    console.log(res);
    this.setData({
      goods: [...this.data.goods, ...res.data.message.goods]
    })
    console.log(this.data.goods);
  },
  goodsadd(e) {
    console.log(e);
    console.log(e.detail, '123213');
    wx.navigateTo({
      url: '/pages/xigoods/xqgoods?goods_id=' + e.detail,
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getdata()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      goods: []
    })
    this.query.pagenum = 1
    this.getdata()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.query.pagenum >= this.totalPages) {
      wx.showToast({
        title: '没有下一页数据'
      });

    } else {
      this.query.pagenum++;
      this.getdata();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})